﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using PPI.Client.Interface;
using PPI.Client.Logic;
using PPI.Client.Logic.Delegate;
using PPI.Client.Logic.XML;
using PPI.Client.Utils;
using PPI.Entities.Settings;

namespace PPI.Client.Presenter
{
    public class DeployDatabasePresenterBase
    {
        protected IDeployDatabase m_view;
        protected Stack<string> m_WebFiles = new Stack<string>();
        protected Stack<string> m_LocalFiles = new Stack<string>();
        protected List<string> m_LocalFilesList = new List<string>();
        private DownloadFile m_Downloader;
        private UpdateBar m_UpdateProgressBar;
        private bool m_DownloadedAllSystemFiles;
        protected ESettingsBase m_SettingsBase;

        public DeployDatabasePresenterBase(IDeployDatabase view)
        {
            m_view = view;
            Initialize();
        }

        private void Initialize()
        {
            try
            {
                m_UpdateProgressBar = new UpdateBar(UpdateProgressDownloadFile);
                m_Downloader = new DownloadFile(m_UpdateProgressBar);
                m_Downloader.FinishDownloadFile += new EventHandler(m_Downloader_FinishDownloadFile);
                m_DownloadedAllSystemFiles = false;

                LoadFilesToDownload();
                StartDownloadFiles();
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }



        private void StartDownloadFiles()
        {
            m_view.ProgressDownloadMessage = "Please wait. Downloading Mobile Files ...";
            if (m_LocalFiles.Count > 0 && m_WebFiles.Count > 0)
            {
                UpdateProgressDownloadFile(0);
                m_Downloader.SetUrl(m_WebFiles.Pop());
                m_Downloader.SetFileDestination(m_LocalFiles.Peek());
                if (File.Exists(m_LocalFiles.Pop()))
                    StartDownloadFiles();
                else
                    m_Downloader.StartDownload();
            }
            else
            {
                m_DownloadedAllSystemFiles = true;
                m_view.ProgressDownloadMessage = "Deployment files are up to date";
                UpdateProgressDownloadFile(100);
                m_view.IsDownloading = false;
                m_view.ActivateDeploy();
            }     
        }

        private void LoadFilesToDownload()
        {
            m_WebFiles.Push(Constants.K_WEBLIBRARYSQLCE);
            m_WebFiles.Push(Constants.K_WEBFRAMEWORKCE);
            m_WebFiles.Push(Constants.K_WEBAPPLICATIONREADME);
            
            m_LocalFiles.Push(Constants.K_LOCALLIBRARYSQLCE);
            m_LocalFiles.Push(Constants.K_LOCALFRAMEWORKCE);
            m_LocalFiles.Push(Constants.K_LOCALAPPLICATIONREADME);
            

            SetApplicationFiles();

            m_LocalFilesList.AddRange(m_LocalFiles);

            CheckFilesAreUpdate();
        }

        protected virtual void CheckFilesAreUpdate()
        {
            throw new NotImplementedException();

        }

        private void UpdateProgressDownloadFile(int percentage)
        {
            m_view.ProgressBarDownload = percentage;            
        }

        protected virtual void SetApplicationFiles()
        {
            throw new NotImplementedException();
        }

        public void DownloadFile(string url, string destination)
        {
            m_view.IsDownloading = true;
            m_view.ProgressDownloadMessage = "Please wait. Downloading Mobile Files ...";
            UpdateProgressDownloadFile(0);
            m_Downloader.SetUrl(url);
            m_Downloader.SetFileDestination(destination);
            m_Downloader.StartDownload();
        }

        public void CopyLocalFilesToDestination(string destinationFolder)
        {
            foreach (string f in m_LocalFilesList)
            {
                FileInfo _FileToCopy = new FileInfo(f);
                if (_FileToCopy.Exists)
                {
                    _FileToCopy.CopyTo(destinationFolder + "\\" + _FileToCopy.Name, true);
                }
            }
        }


        #region Events

        private void m_Downloader_FinishDownloadFile(object sender, EventArgs e)
        {
            try
            {
                if (!m_DownloadedAllSystemFiles)
                    StartDownloadFiles();
                else
                {
                    m_view.IsDownloading = false;
                    m_view.ProgressDownloadMessage = "Deployment files are up to date";
                    UpdateProgressDownloadFile(100);
                    m_view.ShowAnotherInspectionMessage();
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        #endregion
    }
}
